package com.voja.salonschedule;

import java.util.HashMap;
import java.util.Map;

import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class Settings extends PreferenceActivity {
	
	public static final String WORK_HOURS_START_KEY     = "work-hours-start";
	public static final String WORK_HOURS_DEFAULT_START = "8";
	
	public static final String WORK_HOURS_END_KEY       = "work-hours-end";
	public static final String WORK_HOURS_DEFAULT_END   = "20";
	
	public static final String TIME_STEP_KEY     = "time-step";
	public static final String TIME_STEP_DEFAULT = "15";
	
	public static final String SERVICES_KEY     = "services";
	public static final String SERVICES_DEFAULT = "";
	public static final String SERVICES_SPLIT   = ", ";
	
	public static final String CUSTOMERS_KEY     = "customers";
	public static final String CUSTOMERS_DEFAULT = "";
	
	public static final String WIDGET_BACKGROUND = "widget-background";
	public static final String WIDGET_BACKGROUND_DEFAULT = "#000000";
	public static final String WIDGET_BACKGROUND_OPACITY = "widget-background-opacity";
    public static final int WIDGET_BACKGROUND_OPACITY_DEFAULT = 1;
	
	SharedPreferences sharedPrefs;
	Map<String, String> prefFormats = new HashMap<String, String>();
	Resources res;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		res 		= getResources();
		sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
	
		addPreferencesFromResource(R.xml.preferences);
		
		initPref("list", WORK_HOURS_START_KEY, "%s:00", WORK_HOURS_DEFAULT_START);
		initPref("list", WORK_HOURS_END_KEY, "%s:00", WORK_HOURS_DEFAULT_END);
		initPref("list", TIME_STEP_KEY, "%s min.", TIME_STEP_DEFAULT);
		
		setTitle(res.getString(R.string.Settings));
		initServices();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		initServices();
	}
	
	public void initServices() {
		Preference prefServices = initPref("default", SERVICES_KEY, "%s", SERVICES_DEFAULT);
		String services = sharedPrefs.getString(SERVICES_KEY, SERVICES_DEFAULT);
		if (services.equals("")) {
			prefServices.setTitle(res.getString(R.string.NoServices));
		} else {
			int length = services.split(SERVICES_SPLIT).length;
			prefServices.setTitle(res.getString(R.string.Total) + ": " + length);
		}
	}
	
	public Preference initPref(String type, String key, String format, String defaultVal) {
		Preference pref = findPreference(key);
		
		prefFormats.put(key, format);
		pref.setSummary(String.format(format, sharedPrefs.getString(key, defaultVal)));
		
		pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
			@Override
			public boolean onPreferenceChange(Preference pref, Object newValue) {
				String format = prefFormats.get(pref.getKey());
				pref.setSummary(String.format(format, newValue.toString()));
				return true;
			}
		});
		
		return pref;
	}
}
